﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Client.Sys;
using System.Xml.Linq;
using System.Web;
using Model.Sys;
using Sub.Function;

namespace Client.Sys
{
    public class Module: IModule
    {
        public List<ModuleInfo> GetModule(object UserId)
        {
            throw new NotImplementedException();
        }

        public List<ViewInfo> GetViews(ModuleInfo module)
        {
            throw new NotImplementedException();
        }

        public List<MenuInfo> GetListOfMenuInModuleByUser(ModuleInfo Module, string GroupName, object UserId)
        {
            throw new NotImplementedException();
        }

        public void SaveListOfMenuInModuleForUser(object UserId, string GroupKey, IList<MenuInfo> list)
        {
            throw new NotImplementedException();
        }
        /// <summary>
        /// install module from ascx file
        /// </summary>
        /// <param name="action"></param>
        public void InstallModule()
        {
            var sAction = new ActionService();
            var xml = Associate.Render("~/InstallModule/main_module.ascx");
            var doc = XDocument.Parse("<root>" + xml + "</root>");
            var elements = doc.Root.Elements().ToList();
            using (GetUrlService svc = new GetUrlService())
            {
                foreach (var element in elements)
                {              
                         var _main_Module = new main_Module
                        {
                            ModuleName = element.Name.ToString(),
                                Path = "~/" + element.Name.ToString()                                
                        };
                         var SQLInsert = @"INSERT INTO main_Modules(ModuleName, Locked, IconPath,Path)
                                    VALUES     (N'" + _main_Module.ModuleName + "', 0, '','"+ _main_Module.Path + "')";
                            ObjectFunction.ExecuteQuery(SQLInsert, ref sAction);
                                               
              
                }
            }
            
        }
        /// <summary>
        /// install all view module to workspace
        /// </summary>
        /// <param name="module"></param>
        /// <param name="action"></param>
         private void InstallView(main_Module module, ActionService action)
        {
          /*
           var role=HRP.Client.system.DataObjects.Load<aspnet_Role>(@"SELECT     RoleId
                                                                       FROM         aspnet_Roles
                                                                       WHERE     (RoleName = N'system')",ref action);     
           var xml = Utilities.Utils.Render(module.Path  + "/configs/views.ascx");
           var doc = XDocument.Parse("<root>" + xml + "</root>");
           var elements = doc.Root.Elements().ToList();
           foreach (var element in elements)
           {
               var ViewMap = new sys_view_map()
               {
                   ID=Guid.NewGuid(),
                   Path = module.Path+"/"+ element.Attributes().FirstOrDefault(p => p.Name.ToString().ToLower() == "path").Value      
               };
               HRP.Client.system.DataObjects.ExecuteQuery(@"INSERT INTO sys_view_maps(Path, ID) 
                                                           VALUES     (N'"+ViewMap.Path+"','"+ViewMap.ID.ToString()+"')", ref action);

                
               var sys_view_Item = new sys_View()
               {
                   ModuleId =module.ModuleID,
                   ViewPath = ViewMap.Path 
               };
               HRP.Client.system.DataObjects.ExecuteQuery(@"INSERT INTO sys_Views
                                                                   (ViewPath, ModuleId, IsPublic)
                                                                   VALUES     (N'" + ViewMap.Path + "'," + module.ModuleID + ", 0)", ref action);
               sys_view_Item = HRP.Client.system.DataObjects.Load<sys_View>(@"SELECT     ViewId
                                                                           FROM         sys_Views
                                                                           WHERE     (ViewPath = N'" + ViewMap.Path + "')", ref action)[0]; 
               var sys_Role_View_item = new sys_Role_View()
               {
                   AllowAction =true,
                   AllowDelete =true,
                   AllowExport =true,
                   AllowImport =true,
                   AllowInsert=true,
                   AllowPrint =true,
                   AllowUpdate =true,
                   AllowView =true,
                   RoleId = role[0].RoleId,
                   ViewId=sys_view_Item.ViewId  
               };
                HRP.Client.system.DataObjects.ExecuteQuery(@"INSERT INTO sys_Role_Views(ViewId, RoleId, AllowInsert, AllowUpdate, AllowDelete, AllowView, AllowAction, AllowPrint, AllowExport, AllowImport)
                                                        VALUES     (" + sys_view_Item.ViewId.ToString() + ",'" + role[0].RoleId.ToString() + "', 1, 1, 1, 1, 1, 1, 1, 1)", ref action);


 
           }
             */
        }

    }
}
